<template>
  <div>
    <el-dialog title="打印预览" v-model="visible" width="840">
      <div>
        <el-button size="large" type="primary" @click.stop="print">
          打印
        </el-button>
        <el-button size="large" type="primary" @click.stop="toPdf"
          >保存pdf</el-button
        >
      </div>
      <div id="preview_content"></div>
      <template #footer>
        <el-button size="large" @click="hideModal"> 关闭 </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import $ from "jquery";
const visible = ref(false);
const waitShowPrinter = ref(false);
// 纸张宽 mm
const width = ref("0");
// 模板
const hiprintTemplate: any = ref({});
// 数据
const printData = ref({});
const hideModal = () => {
  visible.value = false;
};

const show = (newHiprintTemplate, newPrintData, newWidth = "210") => {
  visible.value = true;
  width.value = newWidth;
  hiprintTemplate.value = newHiprintTemplate;
  printData.value = newPrintData;
  setTimeout(() => {
    // eslint-disable-next-line no-undef
    $("#preview_content").html(newHiprintTemplate.getHtml(newPrintData));
  }, 500);
};

const print = () => {
  waitShowPrinter.value = true;
  hiprintTemplate.value.print(printData.value, {
    callback: () => {
      waitShowPrinter.value = false;
    },
  });
};
const toPdf = () => {
  hiprintTemplate.value.toPdf(printData.value, "打印预览");
};

defineExpose({
  show,
});
</script>
